/**
 * 车辆派遣管理初始化
 */
var Car = {
    id: "CarTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Car.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '出车时间', field: 'cometime', visible: true, align: 'center', valign: 'middle'},
            {title: '回车时间', field: 'gotime', visible: true, align: 'center', valign: 'middle'},
            {title: '用车单位', field: 'unit', visible: true, align: 'center', valign: 'middle'},
            {title: '车号', field: 'number', visible: true, align: 'center', valign: 'middle'},
            {title: '用车人', field: 'people', visible: true, align: 'center', valign: 'middle'},
            {title: '联系电话', field: 'phone', visible: true, align: 'center', valign: 'middle'},
            {title: '批准人', field: 'approval', visible: true, align: 'center', valign: 'middle'},
        {
            title: '状态', visible: true, align: 'center', valign: 'middle', formatter: function (value, row, index) {
                if (row.status == 0) {
                    return '<span  >回库</span>'
                } else if(row.status==3){
                    return '<span  >已预约</span>'
                } else if (row.status==1) {
                    return '<span  >在途中</span>'
                        }
                        else {
                            return '<span  >状态异常</span>'
                }
            }
        }
    ];
};
/**
 * 跳转EX
 */
Car.download = function() {
    window.location.href="/car/download";
}

/**
 * 通过审核
 */


Car.pass = function () {

    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/car/pass", function (data) {
            Feng.success("成功!!!");
            Car.table.refresh();
        }, function (data) {
            Feng.error("失败!!!" + data.responseJSON.message + "!");
        });
        ajax.set("carId",this.seItem.id);
        ajax.start();
    }
};
/**
 * 检查是否选中
 */
Car.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Car.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加车辆派遣
 */
Car.openAddCar = function () {
    var index = layer.open({
        type: 2,
        title: '添加车辆派遣',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/car/car_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看车辆派遣修改
 */
Car.openCarDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '车辆派遣详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/car/car_update/' + Car.seItem.id
        });
        this.layerIndex = index;
    }
};
/**
 * 打开查看车辆派遣详情
 */
Car.details = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '车辆派遣详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/car/details/' + Car.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除车辆派遣
 */
Car.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/car/delete", function (data) {
            Feng.success("删除成功!");
            Car.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("carId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询车辆派遣列表
 */
Car.search = function () {
    var queryData = {};
    queryData['unit'] = $("#unit").val();
    queryData['number'] = $("#number").val();
    Car.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Car.initColumn();
    var table = new BSTable(Car.id, "/car/list", defaultColunms);
    table.setPaginationType("client");
    Car.table = table.init();
});
